local ClientOHOH = _G.ClientOHOH;

local M = ClientOHOH.class.new(script, ClientOHOH.files.OHOHOLYBUIBase());
local CommonOHOH =_G.CommonOHOH
local CallBackManager = ClientOHOH.load.CallBackManager();
local BtnHelper = ClientOHOH.load.BtnHelper();
local PetTool=_G.CommonOHOH.load.PetTool();
local LanguageHelper=_G.LanguageHelper;
local ConnectClass=_G.CommonOHOH.load.Connect();
local GeneralTool=_G.CommonOHOH.load.GeneralTool();
local goods_Cfg=_G.CommonOHOH.config.Goods;
local gid = 501
function InitInsts(ui)
	ui.insts = { }
	ui.insts.root = "ctrl.root"
	ui.insts.close="ctrl.closeBtn" --关闭按钮
	ui.insts.copy="ctrl.slotlist.copy"  --制作槽

	ui.insts.content="ctrl.content.contentFrame" 
	ui.insts.petList="ctrl.petList.peListtFrame" 

	ui.insts.voidnum="ctrl.voidNum" 
	
	ui.insts.voidTime="ctrl.content.time" 
	ui.insts.craftBtn="ctrl.content.btn"

	ui.insts.viewportFrame= "ctrl.slotlist.ViewportFrame"

	ui.insts.price= "ctrl.slotlist.progressPrice"
end;

function M.new(...) 	
	return M(...);
end;
function M.ctor(self)
	InitInsts(self) 
    self.DatabaseReadyEvent = ConnectClass.new()
	self.UpdateSlotListEvent  =ConnectClass.new()
	self.ResetSlotEvent =  ConnectClass.new()
	self.CraftClickEvent=ConnectClass.new()-- 制作
	self.BuyEvent=ConnectClass.new()
	self.BuyDoneEvent=ConnectClass.new()
	self.ReceiveEvent=ConnectClass.new()
	self.CellClickEvent=ConnectClass.new()      -- 宠物List单元被点击
	self.SCaleTimeUpdateSlotListEvent  =ConnectClass.new()
	self.VoidTimeGetEvent  =ConnectClass.new()
	--self.NeedVoidTimeEvent = ConnectClass.new() --需要获取当前VoidTime的时候
	M.super.ctor(self, ClientOHOH:GetPath_Loop("ReplicatedStorage.Resource.UI.PetVoidGui"));

	self.insts.copy.Visible = false
	self.insts.craftBtn.Visible = false
	self.insts.price.Text = goods_Cfg[gid].price

	local e2=_G.CommonOHOH.load.PetTool().GetEvolutionBoostStr(3)
	self:FullText(self.insts.voidnum,{e2.."%%"})

	self:AddProcess(function()
		for _, v in pairs(self.main_inst:GetDescendants()) do
			if v:IsA("GuiButton") then
				BtnHelper:InitButton(v, nil, nil, nil, function() end);
			end;
		end;
	end);

	--关闭按钮
	CallBackManager:AddRbxConnect(self,self.insts.close.Activated, function()
		self:Hide();
		self.insts.copy.Parent.Visible = true
		self.insts.content.Visible = false
		self.insts.petList.Visible = false
	end);
	self:Init()
end;


function M.Init(self)
	self.SlotList= require(script.SlotList).new(self)
	self.List= require(script.List).new(self)  --创建列表
end 


--界面隐藏时 触发
function M.OnHide(self)
	self.List:HideAllCell()
end



function M.MakeClick(self)
	self.insts.copy.Parent.Visible = false
	self.insts.content.Visible = true
	self.insts.petList.Visible = true
end

function M.CellClick(self,cell)
	self.CellClickEvent:Action(cell)
	self.insts.craftBtn.Visible = true
	self.List:Change(cell)
end


function M.voidTimeToHHMMSS(voidTime)
	local hours = math.floor(voidTime / 3600)
	local minutes = math.floor((voidTime % 3600) / 60)
	local seconds = voidTime - hours*3600 - minutes*60
	return string.format("%02d:%02d:%02d", hours, minutes , seconds)
end
function M.Update(self,dt)
	self.needTime=self.needTime-1;

	if(self.needTime==0)then
		self.updateConnect:Disconnect();
		self.updateConnect=nil;
	end
end;
function M.StartUpdateTime(self)
	if self.update_connect then
		return 
	end

	self.update_connect=self:AddStepUpdate(self.Update,1)
end
function M.dtor(self)
	M.super.dtor(self);
end;

function M.SetDataBase(self,dataBase)
	self.DataBase=dataBase
    self.DatabaseReadyEvent:Action()
end

------------------------对外调用接口-----------------------------------------
-- 获取玩家数据库
function M.GetDataBase(self)
	return self.DataBase
end

function M.UpdateHad(self,had)
	self.List:UpdateCell(had)
end
function M.SetVoidTime(self,voidTime)
	-- 计算小时和分钟
	--测试设置的Void时间 记得删除
	self.voidTime = voidTime
	--self.voidTime = 30
	local hours = math.floor(voidTime / 3600)
	local minutes = math.floor((voidTime % 3600) / 60)
	--GeneralTool.secondsToHHMM(min);
	--local timeStr=GeneralTool.secondsToHHMM(min);
	self.insts.voidTime.Text = string.format("time: %dh%dm", hours, minutes)
end
function M.GetVoidTime(self)
	return self.voidTime
end
return M